# -*- coding: utf-8 -*-
# @Time    : 2024/2/23
# @Author  : Yangyali
#
import datetime
import os

import allure
from allure_commons.types import AttachmentType
from loguru import logger
import pytest
import requests
from tools import Global

def pytest_sessionstart(session):
    response = requests.post(Global.base_url+"/account/login", json={"mobile":"13261157199","password":"qWYzq2hW","client":5})
    token = response.json()["data"]["token"]
    logger.info("获取的token为")
    logger.info(token)
    assert response.json()["msg"] == "登录成功"
    assert response.status_code == 200
    Global.token = token
@pytest.fixture(scope="session",autouse=True)
def setlog():
    # 获取项目的根路径
    project_root = os.path.dirname(os.path.abspath(__file__))
    # 要创建的目录名
    directory_name = 'logs'
    # 在项目根路径下创建目录
    new_directory = os.path.join(project_root, directory_name)
    if not os.path.exists(new_directory):
        os.makedirs(new_directory)
    now = datetime.datetime.now()
    # 格式化时间
    formatted_time = now.strftime('%Y%m%d%H%M%S')
    # 输出格式化后的时间
    file_name = formatted_time + ".log"
    file_path = os.path.join(new_directory, file_name)
    print(file_path)
    logger.add(file_path, format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}", level="INFO",backtrace=True, diagnose=True)


# 定义一个装饰器来自动记录接口返回数据
def record_response(test_func):
    def wrapper(*args, **kwargs):
        # 执行测试用例
        response = test_func(*args, **kwargs)

        # 使用allure.attach附加返回数据
        allure.attach(response.json(), name="接口返回数据", attachment_type=allure.attachment_type.JSON)

        return response

    return wrapper


# conftest.py
import pytest
import allure




@pytest.fixture(autouse=True)
def tear_down():
    logger.info("开始执行")
    yield
    # allure.attach(str(response.json()), name="接口返回数据", attachment_type=allure.attachment_type.JSON)
